package org.itboys.mobile.service.mongo.mobile.advertisement;



import org.itboys.framework.resource.ResourceHolder;
import org.itboys.mobile.dto.SundayAdvertisementDto;
import org.itboys.mobile.entity.mongo.SundayAdvertisement;
import org.itboys.mongodb.core.MongoDataSource;
import org.itboys.mongodb.service.BaseMongoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 作者：jiangxiong
 * 日期：2017年5月8日
 * 联系方式 ：empty90911@126.com
 * 描述：圣代_广告信息_service_移动端
 */
@Service
public class SundayMobileAdvertisementService extends BaseMongoService<SundayAdvertisement> {

    @Autowired
    private ResourceHolder resourceHolder;


    @Resource(name = "mobileDs")
    private MongoDataSource kitchenDataSource;
    @Override
    protected MongoDataSource getMongoDataSource() {
        return kitchenDataSource;
    }

    @Override
    protected Class<SundayAdvertisement> getEntityClass() {
        return SundayAdvertisement.class;
    }


    /**
     * 根据广告位获取广告。不分页
     */
   public List<SundayAdvertisementDto> getAdvertisementByZone(String zoneKey){
       Map<String,Object> param = new HashMap<String,Object>();
       param.put("zoneKey",zoneKey);
       param.put("isDeleted",0);
        return pakageDtoMuilt(super.list(param));
   }

    /**
     * 组装DTO
     * @param advertisement
     * @return
     */

    public  SundayAdvertisementDto pakageDtoSingle(SundayAdvertisement advertisement){
       String url=resourceHolder.getStringValue("mobileRoot")+"/sunday/mobile/advertisement/html5?id="+advertisement.getId();
       String info="转跳信息说明。pageType为0，打开Url；pageType为1，打开商品详情，pageId为商品ID；pageType=2,打开商品列表，pageId为商品分类ID；pageType=3,打开商户详情（暂无），pageId为商户ID";
        return new SundayAdvertisementDto(
                advertisement.getId(),advertisement.getCreateTime(),advertisement.getCreateDate(),
                advertisement.getName(),advertisement.getViceName(),advertisement.getImage(), advertisement.getPageType(),
                advertisement.getPageId(),url,info);

    }

    public  List<SundayAdvertisementDto>  pakageDtoMuilt(List<SundayAdvertisement> advertisements){
        List<SundayAdvertisementDto> dtos = new ArrayList<SundayAdvertisementDto>();
        for(SundayAdvertisement advertisement:advertisements){
            dtos.add(pakageDtoSingle(advertisement));
        }
        return dtos;

    }

}
